package com.szly.phm.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.szly.phm.common.entity.dto.fi.*;
import com.szly.phm.common.entity.dto.user.LoginUserDTO;
import com.szly.phm.common.entity.po.*;
import com.szly.phm.common.entity.result.CodeStatusEnum;
import com.szly.phm.common.entity.result.PageResult;
import com.szly.phm.common.entity.result.RootResponse;
import com.szly.phm.common.entity.vo.fi.*;
import com.szly.phm.common.mapper.*;
import com.szly.phm.common.service.IFiService;
import com.szly.phm.common.utils.BeanCopyUtils;
import com.szly.phm.common.utils.DisplayLocationInfoTreeUtil;
import com.szly.phm.common.utils.HandleUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class FiServiceImpl implements IFiService {
        private final FiSubsystemStatusDataMapper fiSubsystemStatusDataMapper;
        private final FiRealTimeFileStructureMapper fiRealTimeFileStructureMapper;
        private final FiDataTypeMapper fiDataTypeMapper;
        private final FiFileTypeMapper fiFileTypeMapper;
        private final RaSystemMapper raSystemMapper;
        private  final RaExtensionMapper raExtensionMapper;
        private final  RaKeyPartsModelMapper raKeyPartsModelMapper;
    private final  RaKeyPartsTypeMapper keyPartsTypeMapper;
        private final FiRadarTypeMapper fiRadarTypeMapper;
        private final FiTaRelationMapper fiTaRelationMapper;
        private final DisplayLocationInfoMapper displayLocationInfoMapper;


    @Override
    public RootResponse<PageResult<DisplayLocationInfoVO>> queryDisplayLocationInfoPage(DisplayLocationInfoPageDTO displayLocationInfoPageDTO) {
        var wrapper = new LambdaQueryWrapper<DisplayLocationInfo>();
        wrapper.eq(DisplayLocationInfo::getIsSoftDelete, false);
        wrapper.orderByAsc(DisplayLocationInfo::getDisplaySerialNum);
        wrapper.isNull(DisplayLocationInfo::getPid);
        // fuzzy query 模糊查询；
        if (displayLocationInfoPageDTO.getName()!=null){
            // if not null, then get the specially query by the name;
            wrapper.like(DisplayLocationInfo::getName,displayLocationInfoPageDTO.getName());
        }
        if(displayLocationInfoPageDTO.getRadarFileType()!=null){
            wrapper.eq(DisplayLocationInfo::getRadarFileType, displayLocationInfoPageDTO.getRadarFileType());

        }
        //
        wrapper.orderByDesc(DisplayLocationInfo::getId,DisplayLocationInfo::getDisplaySerialNum);
        // page helper
        PageHelper.startPage(displayLocationInfoPageDTO.getPageNum(), displayLocationInfoPageDTO.getPageSize());
        var displayLocationInfos = displayLocationInfoMapper.selectList(wrapper);
        var pageInfo = PageInfo.of(displayLocationInfos);
        var pageList = pageInfo.getList();
        var newList=new ArrayList<DisplayLocationInfoVO>();
        pageList.forEach(p->{
            DisplayLocationInfoVO displayLocationInfoVO=new DisplayLocationInfoVO();
            BeanCopyUtils.copy(p,displayLocationInfoVO);
            var wrapper1 = new LambdaQueryWrapper<FiFileType>();
            wrapper1.eq(FiFileType::getIsSoftDelete,0);
            wrapper1.eq(FiFileType::getId,displayLocationInfoVO.getRadarFileType());
            var fiFileType=fiFileTypeMapper.selectOne(wrapper1);
            if(fiFileType!=null){
                displayLocationInfoVO.setRadarFileTypeName(fiFileType.getName());
            }
            newList.add(displayLocationInfoVO);
        });
//        DisplayLocationInfoTreeUtil displayLocationInfoTree=new DisplayLocationInfoTreeUtil(newList);
        return RootResponse.success(PageResult.page(newList, pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal()));
    }
    @Override
    public RootResponse queryDisplayLocationInfosChildPage(DisplayLocationInfoPageChildDTO displayLocationInfoPageDTO) {
        var wrapper = new LambdaQueryWrapper<DisplayLocationInfo>();
        wrapper.eq(DisplayLocationInfo::getIsSoftDelete, false);
        wrapper.orderByAsc(DisplayLocationInfo::getDisplaySerialNum);
        if(displayLocationInfoPageDTO.getName()!=null){
            wrapper.like(DisplayLocationInfo::getName,displayLocationInfoPageDTO.getName());
        }
        if(displayLocationInfoPageDTO.getPid()!=null){
            wrapper.eq(DisplayLocationInfo::getPid, displayLocationInfoPageDTO.getPid());
        }else {
            wrapper.isNull(DisplayLocationInfo::getPid);
        }
        // page helper
        PageHelper.startPage(displayLocationInfoPageDTO.getPageNum(), displayLocationInfoPageDTO.getPageSize());
        var displayLocationInfos = displayLocationInfoMapper.selectList(wrapper);
        var pageInfo = PageInfo.of(displayLocationInfos);
        var pageList = pageInfo.getList();
        var newList=new ArrayList<DisplayLocationInfoChildVO>();
        pageList.forEach(p->{
            DisplayLocationInfoChildVO displayLocationInfoChildVO=new DisplayLocationInfoChildVO();
            BeanCopyUtils.copy(p,displayLocationInfoChildVO);
            var wrapper1 = new LambdaQueryWrapper<FiFileType>();
            wrapper1.eq(FiFileType::getIsSoftDelete,0);
            wrapper1.eq(FiFileType::getId,displayLocationInfoChildVO.getRadarFileType());
            var fiFileType=fiFileTypeMapper.selectOne(wrapper1);
            if(fiFileType!=null){
                displayLocationInfoChildVO.setRadarFileTypeName(fiFileType.getName());
            }
            newList.add(displayLocationInfoChildVO);
        });
        return RootResponse.success(PageResult.page(newList, pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal()));
    }
    @Override
    public RootResponse createDisplayLocationInfo(CreateDisplayLocationInfoDTO createDisplayLocationInfoDTO, LoginUserDTO userDTO) {
        //
        var wrapper = new LambdaQueryWrapper<DisplayLocationInfo>();
        if(createDisplayLocationInfoDTO.getPid()!=null){
            wrapper.eq(DisplayLocationInfo::getPid, createDisplayLocationInfoDTO.getPid());
        }else {
            wrapper.isNull(DisplayLocationInfo::getPid);
        }
        wrapper.eq(DisplayLocationInfo::getRadarFileType, createDisplayLocationInfoDTO.getRadarFileType());
        wrapper.eq(DisplayLocationInfo::getName, createDisplayLocationInfoDTO.getName());
        wrapper.eq(DisplayLocationInfo::getIsSoftDelete, false);
//        var DisplayLocationInfo = DisplayLocationInfoMapper.selectOne(wrapper);
        if (displayLocationInfoMapper.selectOne(wrapper) != null) {

                return RootResponse.error("显示位置已存在，请勿重复添加");

        }
        //
        var displayLocationInfo = new DisplayLocationInfo();
//        BeanCopyUtils.copy(src,dest);
        BeanCopyUtils.copy(createDisplayLocationInfoDTO, displayLocationInfo);
        displayLocationInfo.setId(HandleUtils.UUID32());
        displayLocationInfo.setCreator(userDTO.getUsername());
        displayLocationInfo.setCreateDateTime(new Date());
        displayLocationInfo.setIsSoftDelete(false);
        int i = displayLocationInfoMapper.insert(displayLocationInfo);
        return i == 1 ? RootResponse.success():RootResponse.error();
    }

    @Override
    public RootResponse updateDisplayLocationInfo(UpdateDisplayLocationInfoDTO updateDisplayLocationInfoDTO, LoginUserDTO userDTO) {
        var wrapper1 = new LambdaQueryWrapper<DisplayLocationInfo>();
        wrapper1.eq(DisplayLocationInfo::getName, updateDisplayLocationInfoDTO.getName());
        wrapper1.eq(DisplayLocationInfo::getIsSoftDelete, false);
        if(updateDisplayLocationInfoDTO.getPid()!=null){
            wrapper1.eq(DisplayLocationInfo::getPid, updateDisplayLocationInfoDTO.getPid());
        }else {
            wrapper1.isNull(DisplayLocationInfo::getPid);
        }
        wrapper1.eq(DisplayLocationInfo::getRadarFileType, updateDisplayLocationInfoDTO.getRadarFileType());
        var DisplayLocationInfo1= displayLocationInfoMapper.selectOne(wrapper1);
        if (DisplayLocationInfo1!=null){
            if (!DisplayLocationInfo1.getId().equals(updateDisplayLocationInfoDTO.getId())) {
                //            var DisplayLocationInfo2 = baseMapper.selectOne(wrapper1);
                    return RootResponse.error( "显示位置已存在!!!");
            }
        }
        // the old data:
        var displayLocationInfo_old = displayLocationInfoMapper.selectById(updateDisplayLocationInfoDTO.getId());
        var displayLocationInfo=new DisplayLocationInfo();
        BeanCopyUtils.copy(updateDisplayLocationInfoDTO, displayLocationInfo);
        displayLocationInfo.setUpdater(userDTO.getUsername());
        displayLocationInfo.setUpdateDateTime(new Date());
        int i = displayLocationInfoMapper.updateById(displayLocationInfo);
//        int i=1;
        return i == 1 ? RootResponse.success(displayLocationInfo_old) : RootResponse.error();
    }

    @Override
    public RootResponse deleteDisplayLocationInfo(String id, LoginUserDTO userDTO) {
        // 判断是否被保障基础表关联，关联后不允许被删除喔！
//        var ensureLambdaQueryWrapper = new LambdaQueryWrapper<DisplayLocationInfo>();
////        ensureLambdaQueryWrapper.eq(FaEnsure::getIsSoftDelete,false);
//        ensureLambdaQueryWrapper.eq(DisplayLocationInfo::getLocationId,id);
//        if (displayLocationInfoMapper.selectList(ensureLambdaQueryWrapper)!=null){
//            return RootResponse.error("该级别已被保障基础信息绑定，不能删除");
//        }
        //
        var wrapper1 = new LambdaQueryWrapper<DisplayLocationInfo>();
        wrapper1.eq(DisplayLocationInfo::getIsSoftDelete, false);
        wrapper1.eq(DisplayLocationInfo::getId, id);
        var displayLocationInfo=displayLocationInfoMapper.selectOne(wrapper1);
        if(displayLocationInfo.getPid()!=null){
            var wrapper = new LambdaQueryWrapper<FiRealTimeFileStructure>();
            wrapper.eq(FiRealTimeFileStructure::getIsSoftDelete, false);
            wrapper.eq(FiRealTimeFileStructure::getDisplayLocationId, id);
            if(fiRealTimeFileStructureMapper.selectList(wrapper).size()>0){
                return     RootResponse.error("请先删除此显示位置下的实时文件！！！");
            }
        }else {
            var wrapper2 = new LambdaQueryWrapper<DisplayLocationInfo>();
            wrapper2.eq(DisplayLocationInfo::getIsSoftDelete, false);
            wrapper2.eq(DisplayLocationInfo::getPid, displayLocationInfo.getId());
            var displayLocationInfo1=displayLocationInfoMapper.selectOne(wrapper2);
            if (displayLocationInfo1 != null){
                var wrapper = new LambdaQueryWrapper<FiRealTimeFileStructure>();
                wrapper.eq(FiRealTimeFileStructure::getIsSoftDelete, false);
                wrapper.eq(FiRealTimeFileStructure::getDisplayLocationId, displayLocationInfo1.getId());
                if(fiRealTimeFileStructureMapper.selectList(wrapper).size()>0){
                    return     RootResponse.error("请先删除此显示位置下的实时文件！！！");
                }
            }
        }

        var displayLocationInfoLambdaQueryWrapper = new LambdaQueryWrapper<DisplayLocationInfo>();
        displayLocationInfoLambdaQueryWrapper.eq(DisplayLocationInfo::getIsSoftDelete,false);
        displayLocationInfoLambdaQueryWrapper.eq(DisplayLocationInfo::getId,id);
        var displayLocationInfo1 = displayLocationInfoMapper.selectOne(displayLocationInfoLambdaQueryWrapper);
       while (displayLocationInfo1!=null){
           displayLocationInfo1.setIsSoftDelete(true);
           displayLocationInfo1.setUpdateDateTime(new Date());
           displayLocationInfo1.setUpdater(userDTO.getUsername());
           displayLocationInfoMapper.updateById(displayLocationInfo1);
               displayLocationInfoLambdaQueryWrapper.clear();
               displayLocationInfoLambdaQueryWrapper.eq(DisplayLocationInfo::getIsSoftDelete,false);
               displayLocationInfoLambdaQueryWrapper.eq(DisplayLocationInfo::getPid,displayLocationInfo1.getId());
               displayLocationInfo1 = displayLocationInfoMapper.selectOne(displayLocationInfoLambdaQueryWrapper);
               if(displayLocationInfo1!=null){
                   displayLocationInfo1.setIsSoftDelete(true);
                   displayLocationInfo1.setUpdateDateTime(new Date());
                   displayLocationInfo1.setUpdater(userDTO.getUsername());
                   displayLocationInfoMapper.updateById(displayLocationInfo1);
               }else {
                   break;
               }
       }
        return RootResponse.success(displayLocationInfoMapper.selectById(id));
    }



    @Override
    public RootResponse createFiSubsystemStatusData(CreateFiSubsystemStatusDataDTO createServerAreaDTO, LoginUserDTO userDTO) {

        var wrapper = new LambdaQueryWrapper<FiSubsystemStatusData>();
        wrapper.eq(FiSubsystemStatusData::getIsSoftDelete, false);
        // getStatusSource
        wrapper.eq(FiSubsystemStatusData::getStatusSource,createServerAreaDTO.getStatusSource());
        if (fiSubsystemStatusDataMapper.selectList(wrapper)!=null){
            wrapper.and(wrapper1 -> wrapper1.eq(FiSubsystemStatusData::getCName, createServerAreaDTO.getCName()).or().eq(FiSubsystemStatusData::getEName, createServerAreaDTO.getEName()));
            var taExpertKnowledgeLibrary=fiSubsystemStatusDataMapper.selectOne(wrapper);
            if(taExpertKnowledgeLibrary!=null){
                return RootResponse.error("英文或中文名称已存在！！！");
            }
        }
        var fiSubsystemStatusData= new FiSubsystemStatusData();
        BeanCopyUtils.copy(createServerAreaDTO,fiSubsystemStatusData);
        fiSubsystemStatusData.setId(HandleUtils.UUID32());
        fiSubsystemStatusData.setIsSoftDelete(false);
        fiSubsystemStatusData.setCreator(userDTO.getUsername());
        fiSubsystemStatusData.setCreateDateTime(new Date());
        int i =  fiSubsystemStatusDataMapper.insert(fiSubsystemStatusData);
        return i == 1 ? RootResponse.success() : RootResponse.error();
    }

    @Override
    public RootResponse selectFiSubsystemStatusData(FiSubsystemStatusDataPageDTO serverAreaPageDTO) {
        PageHelper.startPage(serverAreaPageDTO.getPageNum(), serverAreaPageDTO.getPageSize());
        var queryWrapper = new LambdaQueryWrapper<FiSubsystemStatusData>();
        queryWrapper.eq(FiSubsystemStatusData::getIsSoftDelete, false);
        if(serverAreaPageDTO.getName()!=null){
            queryWrapper.and(wrapper1 -> wrapper1.like(FiSubsystemStatusData::getCName, serverAreaPageDTO.getName()).or().like(FiSubsystemStatusData::getEName, serverAreaPageDTO.getName()));
        }
        if(serverAreaPageDTO.getRealTimeFileId()!=null){
            queryWrapper.eq(FiSubsystemStatusData::getStatusSource,serverAreaPageDTO.getRealTimeFileId());
        }
        queryWrapper.orderByDesc(FiSubsystemStatusData::getCreateDateTime);
        var raserverAreas= fiSubsystemStatusDataMapper.selectList(queryWrapper);
        List<FiSubsystemStatusDataVO> lst = new ArrayList<>();
        for (FiSubsystemStatusData statusData : raserverAreas) {
            FiSubsystemStatusDataVO subsystemStatusDataVO = new FiSubsystemStatusDataVO();
            BeanCopyUtils.copy(statusData, subsystemStatusDataVO);
            if (statusData.getKeyPartModelId()!=null){
                subsystemStatusDataVO.setKeyPartModelName(raKeyPartsModelMapper.selectById(statusData.getKeyPartModelId()).getName());
            }
            if (statusData.getRaExtensionId()!=null){
                subsystemStatusDataVO.setRaExtensionName(raExtensionMapper.selectById(statusData.getRaExtensionId()).getName());
            }
            lst.add(subsystemStatusDataVO);
        }

        var pageInfo = PageInfo.of(lst);

        return RootResponse.success(PageResult.page(pageInfo.getList(), pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal()));

    }

    @Override
    public RootResponse deleteFiSubsystemStatusData(String id, LoginUserDTO userDTO) {
        var raserverArea=  new FiSubsystemStatusData();
        raserverArea.setUpdater(userDTO.getUsername());
        raserverArea.setUpdateDateTime(new Date());
        raserverArea.setId(id);
        raserverArea.setIsSoftDelete(true);
        int i =  fiSubsystemStatusDataMapper.updateById(raserverArea);
        return i == 1 ? RootResponse.success(fiSubsystemStatusDataMapper.selectById(id)) : RootResponse.error();
    }

    @Override
    public RootResponse updateFiSubsystemStatusData(UpdateFiSubsystemStatusDataDTO updateServerArea, LoginUserDTO userDTO) {
        var wrapper = new LambdaQueryWrapper<FiSubsystemStatusData>();
        wrapper.eq(FiSubsystemStatusData::getIsSoftDelete, false);
        // getStatusSource
        wrapper.eq(FiSubsystemStatusData::getStatusSource,updateServerArea.getStatusSource());
        if (fiSubsystemStatusDataMapper.selectList(wrapper)!=null){
            wrapper.and(wrapper1 -> wrapper1.eq(FiSubsystemStatusData::getCName, updateServerArea.getCName()).or().eq(FiSubsystemStatusData::getEName, updateServerArea.getEName()));
            var taExpertKnowledgeLibrary=fiSubsystemStatusDataMapper.selectList(wrapper);

            if(taExpertKnowledgeLibrary.size()>0&&!taExpertKnowledgeLibrary.get(0).getId().equals(updateServerArea.getId())){
                return RootResponse.error("英文或中文名称已存在！！！");
            }
        }
        var raserverArea=new FiSubsystemStatusData();
        BeanCopyUtils.copy(updateServerArea,raserverArea);
        raserverArea.setUpdater(userDTO.getUsername());
        raserverArea.setUpdateDateTime(new Date());
        int i =fiSubsystemStatusDataMapper.updateById(raserverArea);
        return i == 1 ? RootResponse.success(fiSubsystemStatusDataMapper.selectById(updateServerArea.getId())) : RootResponse.error();
    }

    @Override
    public RootResponse createFiDataType(CreateFiDataTypeDTO createServerAreaDTO, LoginUserDTO userDTO) {
        var wrapper = new LambdaQueryWrapper<FiDataType>();
        wrapper.eq(FiDataType::getIsSoftDelete, false);
        wrapper.and(wrapper1 -> wrapper1.eq(FiDataType::getName, createServerAreaDTO.getName()).or().eq(FiDataType::getCustomName, createServerAreaDTO.getCustomName()));
        var taExpertKnowledgeLibrary=fiDataTypeMapper.selectOne(wrapper);
        if(taExpertKnowledgeLibrary!=null){
            return RootResponse.error("类型或自定义类型名称已存在！！！");
        }
        var fiSubsystemStatusData= new FiDataType();
        BeanCopyUtils.copy(createServerAreaDTO,fiSubsystemStatusData);
        fiSubsystemStatusData.setIsSoftDelete(false);
        fiSubsystemStatusData.setCreator(userDTO.getUsername());
        fiSubsystemStatusData.setCreateDateTime(new Date());
        int i =  fiDataTypeMapper.insert(fiSubsystemStatusData);
        return i == 1 ? RootResponse.success() : RootResponse.error();
    }

    @Override
    public RootResponse selectFiDataType(FiDataTypePageDTO serverAreaPageDTO) {
            PageHelper.startPage(serverAreaPageDTO.getPageNum(), serverAreaPageDTO.getPageSize());
            var queryWrapper = new LambdaQueryWrapper<FiDataType>();
            queryWrapper.eq(FiDataType::getIsSoftDelete, false);
            if(serverAreaPageDTO.getName()!=null){
                queryWrapper.and(wrapper1 -> wrapper1.like(FiDataType::getCustomName, serverAreaPageDTO.getName()).or().like(FiDataType::getName, serverAreaPageDTO.getName()));
            }
            queryWrapper.orderByDesc(FiDataType::getCreateDateTime);
            var raserverAreas= fiDataTypeMapper.selectList(queryWrapper);
            var pageInfo = PageInfo.of(raserverAreas);

            return RootResponse.success(PageResult.page(pageInfo.getList(), pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal()));

        }

    @Override
    public RootResponse deleteFiDataType(String id, LoginUserDTO userDTO) {
        var wrapper = new LambdaQueryWrapper<FiRealTimeFileStructure>();
        wrapper.eq(FiRealTimeFileStructure::getIsSoftDelete, false);
        wrapper.eq(FiRealTimeFileStructure::getDataTypeId, id);
        if(fiRealTimeFileStructureMapper.selectList(wrapper).size()>0){
            return     RootResponse.error("请先删除此数据类型下的实时文件！！！");
        }
        var raserverArea=  new FiDataType();
        raserverArea.setUpdater(userDTO.getUsername());
        raserverArea.setUpdateDateTime(new Date());
        raserverArea.setId(id);
        raserverArea.setIsSoftDelete(true);
        int i =  fiDataTypeMapper.updateById(raserverArea);
        return i == 1 ? RootResponse.success(fiDataTypeMapper.selectById(id)) : RootResponse.error();

    }

    @Override
    public RootResponse updateFiDataType(UpdateFiDataTypeDTO updateServerArea, LoginUserDTO userDTO) {

        var wrapper = new LambdaQueryWrapper<FiDataType>();
        wrapper.eq(FiDataType::getIsSoftDelete, false);
        wrapper.and(wrapper1 -> wrapper1.eq(FiDataType::getName, updateServerArea.getName()).or().eq(FiDataType::getCustomName, updateServerArea.getCustomName()));
        var taExpertKnowledgeLibrary=fiDataTypeMapper.selectList(wrapper);

        if(taExpertKnowledgeLibrary.size()!=0&&!taExpertKnowledgeLibrary.get(0).getId().equals(updateServerArea.getId())){
            return RootResponse.error(CodeStatusEnum.EXIST_USERNAME.getCode(),"类型或自定义类型名称已存在！！！");
        }
        var raserverArea=new FiDataType();
        BeanCopyUtils.copy(updateServerArea,raserverArea);
        raserverArea.setUpdater(userDTO.getUsername());
        raserverArea.setUpdateDateTime(new Date());
        int i =fiDataTypeMapper.updateById(raserverArea);
        return i == 1 ? RootResponse.success(fiDataTypeMapper.selectById(updateServerArea.getId())) : RootResponse.error();

    }

    @Override
    public RootResponse createFiRealTimeFileStructure(CreateFiRealTimeFileStructureDTO createServerAreaDTO, LoginUserDTO userDTO) {
        var wrapper = new LambdaQueryWrapper<FiRealTimeFileStructure>();
        wrapper.eq(FiRealTimeFileStructure::getIsSoftDelete, false);
        wrapper.eq(FiRealTimeFileStructure::getFileTypeId,createServerAreaDTO.getFileTypeId());
        if (fiRealTimeFileStructureMapper.selectList(wrapper).size()!=0){
            wrapper.and(wrapper1 -> wrapper1.eq(FiRealTimeFileStructure::getCName, createServerAreaDTO.getCName()).or().eq(FiRealTimeFileStructure::getEName, createServerAreaDTO.getEName()));
            var taExpertKnowledgeLibrary=fiRealTimeFileStructureMapper.selectOne(wrapper);
            if(taExpertKnowledgeLibrary!=null){
                return RootResponse.error(CodeStatusEnum.EXIST_USERNAME.getCode(),"英文或中文名称已存在！！！");
            }
        }
        var wrapper1 = new LambdaQueryWrapper<FiRealTimeFileStructure>();
        wrapper1.eq(FiRealTimeFileStructure::getIsSoftDelete, false);
        wrapper1.eq(FiRealTimeFileStructure::getFileTypeId,createServerAreaDTO.getFileTypeId());
        wrapper1.eq(FiRealTimeFileStructure::getDataSerial, createServerAreaDTO.getDataSerial());
        if(fiRealTimeFileStructureMapper.selectList(wrapper1).size()>0){
            return RootResponse.error(CodeStatusEnum.EXIST_USERNAME.getCode(),"同一文件类型下的数据序号不能一样！！！");
        }
        var fiSubsystemStatusData= new FiRealTimeFileStructure();
        BeanCopyUtils.copy(createServerAreaDTO,fiSubsystemStatusData);
        fiSubsystemStatusData.setIsSoftDelete(false);
        fiSubsystemStatusData.setCreator(userDTO.getUsername());
        fiSubsystemStatusData.setCreateDateTime(new Date());
        int i =  fiRealTimeFileStructureMapper.insert(fiSubsystemStatusData);
        return i == 1 ? RootResponse.success() : RootResponse.error();
    }

    @Override
    public RootResponse selectFiRealTimeFileStructure(FiRealTimeFileStructurePageDTO serverAreaPageDTO) {
        if("01".equals(serverAreaPageDTO.getType())){
            var newList=new ArrayList<FiRealTimeFileStructureVO>();
            var queryWrapper = new LambdaQueryWrapper<FiRealTimeFileStructure>();
            queryWrapper.eq(FiRealTimeFileStructure::getIsSoftDelete, false);
            queryWrapper.orderByAsc(FiRealTimeFileStructure::getDataSerial);
            if(serverAreaPageDTO.getName()!=null){
                queryWrapper.and(wrapper1 -> wrapper1.like(FiRealTimeFileStructure::getEName, serverAreaPageDTO.getName()).or().like(FiRealTimeFileStructure::getCName, serverAreaPageDTO.getName()));
            }
            if(serverAreaPageDTO.getFileTypeId()!=null){
                queryWrapper.eq(FiRealTimeFileStructure::getFileTypeId, serverAreaPageDTO.getFileTypeId());
            }
            if(serverAreaPageDTO.getDataTypeId()!=null){
                queryWrapper.eq(FiRealTimeFileStructure::getDataTypeId, serverAreaPageDTO.getDataTypeId());
            }
            if(serverAreaPageDTO.getIsStatus()!=null){
                queryWrapper.eq(FiRealTimeFileStructure::getIsStatus, serverAreaPageDTO.getIsStatus());
            }
            if(serverAreaPageDTO.getSubsystemId()!=null){
                queryWrapper.eq(FiRealTimeFileStructure::getSystemId, serverAreaPageDTO.getSubsystemId());
            }
            queryWrapper.orderByDesc(FiRealTimeFileStructure::getCreateDateTime);
            var fiRealTimeFileStructureVOS= fiRealTimeFileStructureMapper.selectAllFiRealTimeFileStructure(serverAreaPageDTO);
            return RootResponse.success(fiRealTimeFileStructureVOS);

        }else {
            var newList=new ArrayList<FiRealTimeFileStructureVO>();
            PageHelper.startPage(serverAreaPageDTO.getPageNum(), serverAreaPageDTO.getPageSize());
            var queryWrapper = new LambdaQueryWrapper<FiRealTimeFileStructure>();
            queryWrapper.orderByAsc(FiRealTimeFileStructure::getDataSerial);
            queryWrapper.eq(FiRealTimeFileStructure::getIsSoftDelete, false);
            if(serverAreaPageDTO.getName()!=null){
                queryWrapper.and(wrapper1 -> wrapper1.like(FiRealTimeFileStructure::getEName, serverAreaPageDTO.getName()).or().like(FiRealTimeFileStructure::getCName, serverAreaPageDTO.getName()));
            }
            if(serverAreaPageDTO.getFileTypeId()!=null){
                queryWrapper.eq(FiRealTimeFileStructure::getFileTypeId, serverAreaPageDTO.getFileTypeId());
            }
            if(serverAreaPageDTO.getDataTypeId()!=null){
                queryWrapper.eq(FiRealTimeFileStructure::getDataTypeId, serverAreaPageDTO.getDataTypeId());
            }
            if(serverAreaPageDTO.getIsStatus()!=null){
                queryWrapper.eq(FiRealTimeFileStructure::getIsStatus, serverAreaPageDTO.getIsStatus());
            }
            if(serverAreaPageDTO.getSubsystemId()!=null){
                queryWrapper.eq(FiRealTimeFileStructure::getSystemId, serverAreaPageDTO.getSubsystemId());
            }
            queryWrapper.orderByDesc(FiRealTimeFileStructure::getCreateDateTime);
            var raserverAreas= fiRealTimeFileStructureMapper.selectList(queryWrapper);
            var pageInfo = PageInfo.of(raserverAreas);
            pageInfo.getList().forEach(p->{
                var fiRealTimeFileStructureVO=new FiRealTimeFileStructureVO();
                BeanCopyUtils.copy(p,fiRealTimeFileStructureVO);
                if(fiRealTimeFileStructureVO.getSystemId()!=null){
                    var queryWrapper1 = new LambdaQueryWrapper<RaSystem>();
                    queryWrapper1.eq(RaSystem::getId,fiRealTimeFileStructureVO.getSystemId());
                    queryWrapper1.eq(RaSystem::getIsSoftDelete,false);
                   var raSystem= raSystemMapper.selectOne(queryWrapper1);
                    fiRealTimeFileStructureVO.setSystemName(raSystem==null?null:raSystem.getName());
                }
                if(fiRealTimeFileStructureVO.getKeyPartsModelId()!=null){
                    var queryWrapper1 = new LambdaQueryWrapper<RaKeyPartsModel>();
                    queryWrapper1.eq(RaKeyPartsModel::getId,fiRealTimeFileStructureVO.getKeyPartsModelId());
                    queryWrapper1.eq(RaKeyPartsModel::getIsSoftDelete,false);
                    var raKeyPartsModel=raKeyPartsModelMapper.selectOne(queryWrapper1);
                    fiRealTimeFileStructureVO.setKeyPartsModelName(raKeyPartsModel==null?null:keyPartsTypeMapper.selectById(raKeyPartsModel.getTypeId())+raKeyPartsModel.getName());
                }
                if(fiRealTimeFileStructureVO.getExtensionId()!=null){
                    var queryWrapper1 = new LambdaQueryWrapper<RaExtension>();
                    queryWrapper1.eq(RaExtension::getId,fiRealTimeFileStructureVO.getExtensionId());
                    queryWrapper1.eq(RaExtension::getIsSoftDelete,false);
                   var raExtension= raExtensionMapper.selectOne(queryWrapper1);
                    fiRealTimeFileStructureVO.setExtensionName(raExtension==null?null:raExtension.getName());
                }
                if(fiRealTimeFileStructureVO.getDataTypeId()!=null){
                    var queryWrapper1 = new LambdaQueryWrapper<FiDataType>();
                    queryWrapper1.eq(FiDataType::getId,fiRealTimeFileStructureVO.getDataTypeId());
                    queryWrapper1.eq(FiDataType::getIsSoftDelete,false);
                   var fiDataType= fiDataTypeMapper.selectOne(queryWrapper1);
                    fiRealTimeFileStructureVO.setDataTypeName(fiDataType==null?null:fiDataType.getName());
                }
                if(fiRealTimeFileStructureVO.getFileTypeId()!=null){
                    var queryWrapper1 = new LambdaQueryWrapper<FiFileType>();
                    queryWrapper1.eq(FiFileType::getIsSoftDelete,false);
                    queryWrapper1.eq(FiFileType::getId,fiRealTimeFileStructureVO.getFileTypeId());
                    var fiFileType=fiFileTypeMapper.selectOne(queryWrapper1);
                    fiRealTimeFileStructureVO.setFileTypeName(fiFileType==null?null:fiFileType.getName());
                }
                if (fiRealTimeFileStructureVO.getDisplayLocationId()!=null){
                    var displayLocationVO= displayLocationInfoMapper.selectDisLocationInfo(fiRealTimeFileStructureVO.getDisplayLocationId());
                    fiRealTimeFileStructureVO.setDisplayLocationName(displayLocationVO==null?null:displayLocationVO.getDisplayLocationName());
                    fiRealTimeFileStructureVO.setDisplayLocationId(displayLocationVO==null?null:displayLocationVO.getDisplayLocationId());
                    fiRealTimeFileStructureVO.setDisplayLocationPName(displayLocationVO==null?null:displayLocationVO.getDisplayLocationPName());
                    fiRealTimeFileStructureVO.setDisplayLocationPId(displayLocationVO==null?null:displayLocationVO.getDisplayLocationPId());
                }
                newList.add(fiRealTimeFileStructureVO);
            });
            return RootResponse.success(PageResult.page(newList, pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal()));

        }
    }

    @Override
    public RootResponse selectRealTimeFileStructures(String modelId) {
        var list=new ArrayList<String>();
        var wrapper = new LambdaQueryWrapper<FiTaRelation>();
        wrapper.eq(FiTaRelation::getIsSoftDelete, false);
        wrapper.eq(FiTaRelation::getModelId,modelId);
        var taExpertKnowledgeLibrary=fiTaRelationMapper.selectList(wrapper);
        taExpertKnowledgeLibrary.forEach(f->{
            list.add(f.getRealId());

        });
        return RootResponse.success(list);
    }

    @Override
    public RootResponse deleteFiRealTimeFileStructure(String id, LoginUserDTO userDTO) {
        var raserverArea=  new FiRealTimeFileStructure();
        raserverArea.setUpdater(userDTO.getUsername());
        raserverArea.setUpdateDateTime(new Date());
        raserverArea.setId(id);
        raserverArea.setIsSoftDelete(true);
        int i =  fiRealTimeFileStructureMapper.updateById(raserverArea);
        return i == 1 ? RootResponse.success(fiRealTimeFileStructureMapper.selectById(id)) : RootResponse.error();

    }

    @Override
    public RootResponse updateFiRealTimeFileStructure(UpdateFiRealTimeFileStructureDTO updateServerArea, LoginUserDTO userDTO) {
        var wrapper = new LambdaQueryWrapper<FiRealTimeFileStructure>();
        wrapper.eq(FiRealTimeFileStructure::getIsSoftDelete, false);
        wrapper.eq(FiRealTimeFileStructure::getFileTypeId,updateServerArea.getFileTypeId());
        if (fiRealTimeFileStructureMapper.selectList(wrapper).size()!=0){
            wrapper.eq(FiRealTimeFileStructure::getEName, updateServerArea.getEName());
            var taExpertKnowledgeLibrary=fiRealTimeFileStructureMapper.selectList(wrapper);
            if(taExpertKnowledgeLibrary.size()!=0&&!taExpertKnowledgeLibrary.get(0).getId().equals(updateServerArea.getId())){
                return RootResponse.error(CodeStatusEnum.EXIST_USERNAME.getCode(),"英文名称已存在！！！");
            }
        }
        var wrapper1 = new LambdaQueryWrapper<FiRealTimeFileStructure>();
        wrapper1.eq(FiRealTimeFileStructure::getIsSoftDelete, false);
        wrapper1.eq(FiRealTimeFileStructure::getFileTypeId,updateServerArea.getFileTypeId());
        wrapper1.eq(FiRealTimeFileStructure::getDataSerial, updateServerArea.getDataSerial());
       var fiRealTimeFileStructures= fiRealTimeFileStructureMapper.selectList(wrapper1);
        if(fiRealTimeFileStructures.size()>0&&!fiRealTimeFileStructures.get(0).getId().equals(updateServerArea.getId())){
            return RootResponse.error(CodeStatusEnum.EXIST_USERNAME.getCode(),"同一文件类型下的数据序号不能一样！！！");
        }
        var raserverArea=new FiRealTimeFileStructure();
        BeanCopyUtils.copy(updateServerArea,raserverArea);
        raserverArea.setUpdater(userDTO.getUsername());
        raserverArea.setUpdateDateTime(new Date());
        int i =fiRealTimeFileStructureMapper.updateById(raserverArea);
        return i == 1 ? RootResponse.success(fiRealTimeFileStructureMapper.selectById(updateServerArea.getId())) : RootResponse.error();

    }

    @Override
    public RootResponse createFiFileType(CreateFiFileTypeDTO createServerAreaDTO, LoginUserDTO userDTO) {
        var wrapper = new LambdaQueryWrapper<FiFileType>();
        wrapper.eq(FiFileType::getIsSoftDelete, false);
        wrapper.eq(FiFileType::getName,createServerAreaDTO.getName());
        var taExpertKnowledgeLibrary=fiFileTypeMapper.selectOne(wrapper);
        if(taExpertKnowledgeLibrary!=null){
            return RootResponse.error(CodeStatusEnum.EXIST_USERNAME.getCode(),"名称已存在！！！");
        }
        var fiSubsystemStatusData= new FiFileType();
        BeanCopyUtils.copy(createServerAreaDTO,fiSubsystemStatusData);
        fiSubsystemStatusData.setId(HandleUtils.UUID32());
        fiSubsystemStatusData.setIsSoftDelete(false);
        fiSubsystemStatusData.setCreator(userDTO.getUsername());
        fiSubsystemStatusData.setCreateDateTime(new Date());
        int i =  fiFileTypeMapper.insert(fiSubsystemStatusData);
        return i == 1 ? RootResponse.success() : RootResponse.error();
    }

    @Override
    public RootResponse selectFiFileType(FiFileTypePageDTO serverAreaPageDTO) {
        var newList=new ArrayList<FiFileTypeVO>();
        PageHelper.startPage(serverAreaPageDTO.getPageNum(), serverAreaPageDTO.getPageSize());
        var queryWrapper = new LambdaQueryWrapper<FiFileType>();
        queryWrapper.eq(FiFileType::getIsSoftDelete, false);
        if(serverAreaPageDTO.getName()!=null){
            queryWrapper.like(FiFileType::getName,serverAreaPageDTO.getName());
        }
        queryWrapper.orderByDesc(FiFileType::getCreateDateTime);
        var raserverAreas= fiFileTypeMapper.selectList(queryWrapper);
        var pageInfo = PageInfo.of(raserverAreas);
        pageInfo.getList().forEach(p->{
            var fiFileTypeVO=new FiFileTypeVO();
            BeanCopyUtils.copy(p,fiFileTypeVO);
            var wrapper = new LambdaQueryWrapper<FiRadarType>();
            wrapper.eq(FiRadarType::getIsSoftDelete, false);
            wrapper.eq(FiRadarType::getId,fiFileTypeVO.getRadarTypeId());
          var fiRadarType=  fiRadarTypeMapper.selectOne(wrapper);
            fiFileTypeVO.setRadarTypeName(fiRadarType==null?null:fiRadarType.getName());
            newList.add(fiFileTypeVO);
        });
        return RootResponse.success(PageResult.page(newList, pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal()));

    }

    @Override
    public RootResponse deleteFiFileType(String id, LoginUserDTO userDTO) {
        var wrapper = new LambdaQueryWrapper<FiRealTimeFileStructure>();
        wrapper.eq(FiRealTimeFileStructure::getIsSoftDelete, false);
        wrapper.eq(FiRealTimeFileStructure::getFileTypeId, id);
        if(fiRealTimeFileStructureMapper.selectList(wrapper).size()>0){
            return     RootResponse.error("请先删除此文件类型下的实时文件！！！");
        }
        var raserverArea=  new FiFileType();
        raserverArea.setUpdater(userDTO.getUsername());
        raserverArea.setUpdateDateTime(new Date());
        raserverArea.setId(id);
        raserverArea.setIsSoftDelete(true);
        int i =  fiFileTypeMapper.updateById(raserverArea);
        return i == 1 ? RootResponse.success(fiFileTypeMapper.selectById(id)) : RootResponse.error();

    }

    @Override
    public RootResponse updateFiFileType(UpdateFiFileTypeDTO updateServerArea, LoginUserDTO userDTO) {
        var wrapper = new LambdaQueryWrapper<FiFileType>();
        wrapper.eq(FiFileType::getIsSoftDelete, false);
        wrapper.eq(FiFileType::getName,updateServerArea.getName());
        var taExpertKnowledgeLibrary=fiFileTypeMapper.selectOne(wrapper);
        if(taExpertKnowledgeLibrary!=null&&!taExpertKnowledgeLibrary.getId().equals(updateServerArea.getId())){
            return RootResponse.error(CodeStatusEnum.EXIST_USERNAME.getCode(),"名称已存在！！！");
        }
        var raserverArea=new FiFileType();
        BeanCopyUtils.copy(updateServerArea,raserverArea);
        raserverArea.setUpdater(userDTO.getUsername());
        raserverArea.setUpdateDateTime(new Date());
        int i =fiFileTypeMapper.updateById(raserverArea);
        return i == 1 ? RootResponse.success(fiFileTypeMapper.selectById(updateServerArea.getId())) : RootResponse.error();
    }

    @Override
    public RootResponse createFiTaReLation(CreateFiTaRelation createRaInterventionTypeDTO, LoginUserDTO userDTO) {

        // 先删除角色和权限的关联关系
        var wrapper = new LambdaQueryWrapper<FiTaRelation>();
        wrapper.eq(FiTaRelation::getModelId, createRaInterventionTypeDTO.getModelId());
        fiTaRelationMapper.delete(wrapper);
        // 添加关联关系
        if (createRaInterventionTypeDTO.getRealFileIds() != null && !createRaInterventionTypeDTO.getRealFileIds().isEmpty()) {
            createRaInterventionTypeDTO.getRealFileIds().forEach(m -> {
                var sysRolePermission= new FiTaRelation();
                sysRolePermission.setId(HandleUtils.UUID32());
                sysRolePermission.setCreator(userDTO.getUsername());
                sysRolePermission.setCreateDateTime(new Date());
                sysRolePermission.setRealId(m);
                sysRolePermission.setIsSoftDelete(false);
                sysRolePermission.setModelId(createRaInterventionTypeDTO.getModelId());
                int j=fiTaRelationMapper.insert(sysRolePermission);
                if (j != 1) {
                    throw new RuntimeException("失败");
                }
            });
        }
        return RootResponse.success();
    }


}
